package com.itshen.config;

import com.itshen.context.BaseContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    /**
     * 设置Feign日志级别
     */
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
       //注册拦截器，发送feign请求前将安全上下文添加到http请求头，传递给下游微服务
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                // 获取当前用户的ID
                Long userId = BaseContext.getCurrentId();
                // 如果用户ID不为空
                if(userId != null){
                    // 将用户ID添加到HTTP请求头中
                    requestTemplate.header("user-info",userId.toString());
                }
            }
        };
    }
}
